﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace FinallyVideoCaptureTest
{
    public class Db4oProvider
    {
        public Db4oProvider(string dataBaseFile)
        {
            this.uriOfDb = dataBaseFile;
        }

        public void StartServer()
        {
            if (this.server == null)
            {
                this.server = Db4objects.Db4o.CS.Db4oClientServer.OpenServer(this.uriOfDb, 0);
            }
        }

        public void StopServer()
        {
            if (this.server != null)
            {
                this.server.Close();
            }
        }

        public void SaveFrame(Frame frame)
        {
            using (var container = OpenContainer())
            {
                container.Store(frame);
            }
        }

        public IList<Frame> GetFrames(DateTime sTime, DateTime eTime)
        {
            using (var container = OpenContainer())
            {
                return container.Query<Frame>(frame =>
                {
                    return frame.CaptureTime >= sTime && frame.CaptureTime <= eTime;
                });
            }
        }

        public IList<Frame> GetFrames()
        {
            using (var container = OpenContainer())
            {
                return container.Query<Frame>();
            }
        }

        private Db4objects.Db4o.IObjectContainer OpenContainer()
        {
            return this.server.OpenClient();
        }

        private string uriOfDb;
        private Db4objects.Db4o.IObjectServer server;
    }
}
